####
#Local News Attacks
####

library(plyr)
library(xtable)
library(stargazer)
load('localtrust.combined.RData')

##
#Main outcomes - Descriptive Appendix Tables
##

trust.summary <- ddply(localtrust.combined,.(treatment),summarise,trust.mean=mean(local.newspaper.trust,na.rm=TRUE),trust.sd=sd(local.newspaper.trust,na.rm=TRUE),n=length(local.newspaper.trust))

politician.summary <- ddply(localtrust.combined,.(treatment),summarise,polapproval.mean=mean(politician.approval.scale,na.rm=TRUE),trust.sd=sd(politician.approval.scale,na.rm=TRUE),n=length(politician.approval.scale))

use.summary <- ddply(localtrust.combined,.(treatment),summarise,use.mean=mean(local.newspaper.use,na.rm=TRUE),trust.sd=sd(local.newspaper.use,na.rm=TRUE),n=length(local.newspaper.use))

names(trust.summary) <- c('Variable/Condition','mean','sd','n')
trust.summary[,1] <- c('Newspaper Trust Index - Control','Newspaper Trust Index - Treatment')

names(use.summary) <- c('Variable/Condition','mean','sd','n')
use.summary[,1] <- c('Newspaper Use Index - Control','Newspaper Use Index - Treatment')

names(politician.summary) <- c('Variable/Condition','mean','sd','n')
politician.summary[,1] <- c('Politician Approval Index - Control','Politician Approval Index - Treatment')

combined.table <- rbind.data.frame(trust.summary,use.summary,politician.summary)

#Appendix Table A8
xtable(combined.table,digits=2)

###
#Pre-Treatment DVs - Descriptive Appendix Tables
###

trust.summary.pre <- ddply(localtrust.combined,.(treatment),summarise,trust.mean=mean(newspaper.reputation.pre,na.rm=TRUE),trust.sd=sd(newspaper.reputation.pre,na.rm=TRUE),n=length(newspaper.reputation.pre))

politician.summary.pre <- ddply(localtrust.combined,.(treatment),summarise,polapproval.mean=mean(politician.reputation.pre,na.rm=TRUE),trust.sd=sd(politician.reputation.pre,na.rm=TRUE),n=length(politician.reputation.pre))

use.summary.pre <- ddply(localtrust.combined,.(treatment),summarise,use.mean=mean(newspaper.use.pre,na.rm=TRUE),trust.sd=sd(newspaper.use.pre,na.rm=TRUE),n=length(newspaper.use.pre))

names(trust.summary.pre) <- c('Variable/Condition','mean','sd','n')
trust.summary.pre[,1] <- c('Newspaper Trust (Pre) - Control','Newspaper Trust (Pre) - Treatment')

names(use.summary.pre) <- c('Variable/Condition','mean','sd','n')
use.summary.pre[,1] <- c('Newspaper Use (Pre) - Control','Newspaper Use (Pre) - Treatment')

names(politician.summary.pre) <- c('Variable/Condition','mean','sd','n')
politician.summary.pre[,1] <- c('Politician Approval (Pre) - Control','Politician Approval (Pre)- Treatment')

combined.table.pre <- rbind.data.frame(trust.summary.pre,use.summary.pre,politician.summary.pre)

#Appendix Table A7
xtable(combined.table.pre,digits=2) 

###
#Informational Manipulation Check
###

manipulation.check.all <- mean(localtrust.combined$correct.manip.check,na.rm=TRUE)
manipulation.check.prolific <- mean(localtrust.combined$correct.manip.check[which(localtrust.combined$survey.type=="prolific")],na.rm=TRUE)
manipulation.check.cloud <- mean(localtrust.combined$correct.manip.check[which(localtrust.combined$survey.type=="cloud")],na.rm=TRUE)

manipulation.check <- c(manipulation.check.all,manipulation.check.prolific,manipulation.check.cloud)
survey.types <- c('All','Prolific','CloudResearch')

manip.table <- cbind.data.frame(survey.types,manipulation.check)

#Appendix Table A2
xtable(manip.table)

###
#Pre-Treatment Media Outlet Trust Comparison (Footnote 1)
###
round(mean(localtrust.combined$cnn.reputation.pre,na.rm=TRUE),digits=2)
round(mean(localtrust.combined$fox.reputation.pre,na.rm=TRUE),digits=2)
round(mean(localtrust.combined$usatoday.reputation.pre,na.rm=TRUE),digits=2)
round(mean(localtrust.combined$newspaper.reputation.pre,na.rm=TRUE),digits=2)


###
#Main Effects
###

news.trust.main <- lm(local.newspaper.trust ~ treatment + factor(newspaper.reputation.pre), data=localtrust.combined)
news.use.main <- lm(local.newspaper.use ~ treatment + factor(newspaper.use.pre), data=localtrust.combined)
news.trust.party <- lm(local.newspaper.trust ~ treatment*I(pid3pt_scale=="Republican") + factor(newspaper.reputation.pre), data=localtrust.combined)
news.use.party <- lm(local.newspaper.use ~ treatment*I(pid3pt_scale=="Republican") + factor(newspaper.use.pre), data=localtrust.combined)

#Table 1
stargazer(news.trust.main,news.trust.party,news.use.main,news.use.party,keep=c('treatment','Republican'),covariate.labels="Local News Attack",dep.var.labels=c("Media Reputation","Media Use"),digits=1)

###
#Marginal Effect Among Democrats (Reputation and Use)
###

news.trust.dem.point <- news.trust.party$coefficients[2]
news.trust.dem.se <- sqrt(vcov(news.trust.party)[2,2]) 
news.trust.dem.upper <- news.trust.dem.point + 2*news.trust.dem.se
news.trust.dem.lower <- news.trust.dem.point - 2*news.trust.dem.se

news.trust.dem.point; news.trust.dem.lower; news.trust.dem.upper

news.use.dem.point <- news.use.party$coefficients[2]
news.use.dem.se <- sqrt(vcov(news.use.party)[2,2]) 
news.use.dem.upper <- news.use.dem.point + 2*news.use.dem.se
news.use.dem.lower <- news.use.dem.point - 2*news.use.dem.se

news.use.dem.point; news.use.dem.lower; news.use.dem.upper

###
#Marginal Effect Among Republicans (Reputation and Use)
###
news.trust.rep.point <- news.trust.party$coefficients[2] + news.trust.party$coefficients[8]
news.trust.rep.se <- sqrt(vcov(news.trust.party)[2,2] + vcov(news.trust.party)[8,8] + 2*vcov(news.trust.party)[2,8]) 
news.trust.rep.upper <- news.trust.rep.point + 2*news.trust.rep.se
news.trust.rep.lower <- news.trust.rep.point - 2*news.trust.rep.se

news.trust.rep.point; news.trust.rep.lower; news.trust.rep.upper

news.use.rep.point <- news.use.party$coefficients[2] + news.use.party$coefficients[8]
news.use.rep.se <- sqrt(vcov(news.use.party)[2,2] + vcov(news.use.party)[8,8] + 2*vcov(news.use.party)[2,8]) 
news.use.rep.upper <- news.use.rep.point + 2*news.use.rep.se
news.use.rep.lower <- news.use.rep.point - 2*news.use.rep.se

news.use.rep.point; news.use.rep.lower; news.use.rep.upper

###
#Robustness Test of Results by survey vendor
###

#Attack on Newspaper Trust - CloudResearch
news.trust.m.c <- lm(local.newspaper.trust ~ treatment + factor(newspaper.reputation.pre), data=localtrust.combined,subset=which(survey.type=='cloud'))
news.use.m.c <- lm(local.newspaper.use ~ treatment + factor(newspaper.use.pre), data=localtrust.combined,subset=which(survey.type=='cloud'))
news.trust.p.c <- lm(local.newspaper.trust ~ treatment*I(pid3pt_scale=="Republican") + factor(newspaper.reputation.pre), data=localtrust.combined,subset=which(survey.type=='cloud'))
news.use.p.c <- lm(local.newspaper.use ~ treatment*I(pid3pt_scale=="Republican") + factor(newspaper.use.pre), data=localtrust.combined,subset=which(survey.type=='cloud'))

#Table E2
stargazer(news.trust.m.c,news.use.m.c,news.trust.p.c,news.use.p.c,keep=c('treatment','Republican'),covariate.labels="Local News Attack",dep.var.labels=c("Media Reputation","Media Use"),digits=1)

#Attack on Newspaper Trust - Prolific
news.trust.m.p <- lm(local.newspaper.trust ~ treatment + factor(newspaper.reputation.pre), data=localtrust.combined,subset=which(survey.type=='prolific'))
news.use.m.p <- lm(local.newspaper.use ~ treatment + factor(newspaper.use.pre), data=localtrust.combined,subset=which(survey.type=='prolific'))
news.trust.p.p <- lm(local.newspaper.trust ~ treatment*I(pid3pt_scale=="Republican") + factor(newspaper.reputation.pre), data=localtrust.combined,subset=which(survey.type=='prolific'))
news.use.p.p <- lm(local.newspaper.use ~ treatment*I(pid3pt_scale=="Republican") + factor(newspaper.use.pre), data=localtrust.combined,subset=which(survey.type=='prolific'))

#Table E1
stargazer(news.trust.m.p,news.use.m.p,news.trust.p.p,news.use.p.p,keep=c('treatment','Republican'),covariate.labels="Local News Attack",dep.var.labels=c("Media Reputation","Media Use"),digits=1)

pol.approval.main <- lm(politician.approval.scale ~ treatment + factor(politician.reputation.pre), data=localtrust.combined)
pol.approval.party <- lm(politician.approval.scale ~ treatment*I(pid3pt_scale=="Republican") + factor(politician.reputation.pre), data=localtrust.combined)

#Table C2
stargazer(pol.approval.main,pol.approval.party,keep=c('treatment','Republican'),covariate.labels=c('Local News Attack','Republican','Local News Attack X Republican'),column.labels=c("Main","PID as Moderator"),digits=1)

#Table E4
pol.approval.m.c <- lm(politician.approval.scale ~ treatment + factor(politician.reputation.pre), data=localtrust.combined,subset=which(survey.type=="cloud"))
pol.approval.p.c <- lm(politician.approval.scale ~ treatment*I(pid3pt_scale=="Republican") + factor(politician.reputation.pre), data=localtrust.combined,,subset=which(survey.type=="cloud"))

stargazer(pol.approval.m.c,pol.approval.p.c,keep=c('treatment','Republican'),covariate.labels=c('Local News Attack','Republican','Local News Attack X Republican'),column.labels=c("Main","PID as Moderator"),digits=1)

#Table E3
pol.approval.m.p <- lm(politician.approval.scale ~ treatment + factor(politician.reputation.pre), data=localtrust.combined,subset=which(survey.type=="prolific"))
pol.approval.p.p <- lm(politician.approval.scale ~ treatment*I(pid3pt_scale=="Republican") + factor(politician.reputation.pre), data=localtrust.combined,,subset=which(survey.type=="prolific"))

stargazer(pol.approval.m.p,pol.approval.p.p,keep=c('treatment','Republican'),covariate.labels=c('Local News Attack','Republican','Local News Attack X Republican'),column.labels=c("Main","PID as Moderator"),digits=1)

#Familiarity as Moderator
news.trust.fam.int <- lm(local.newspaper.trust ~ treatment*newspaper.familiarity + factor(newspaper.reputation.pre), data=localtrust.combined)
news.use.fam.int <- lm(local.newspaper.use ~ treatment*newspaper.familiarity + factor(newspaper.use.pre), data=localtrust.combined)
pol.approval.fam.int <- lm(politician.approval.scale ~ treatment*newspaper.familiarity + factor(politician.reputation.pre), data=localtrust.combined)

#Table G2
stargazer(news.trust.fam.int,news.use.fam.int,keep=c('treatment','newspaper.familiarity'),covariate.labels=c('Local News Attack','Familiar','Local News Attack X Familiar'),dep.var.labels=c("Newspaper Reputation","Newspaper Use"),digits=1)

##
#Visuals for Figure 2
##

#Familiarity and Trust

familiarity.interval <- seq(from=min(localtrust.combined$newspaper.familiarity),to=max(localtrust.combined$newspaper.familiarity),length=100)
news.trust.fam.int.marginal.point <- news.trust.fam.int$coefficients[2] + news.trust.fam.int$coefficients[8]*familiarity.interval
news.trust.fam.int.marginal.se <- sqrt(vcov(news.trust.fam.int)[2,2] + familiarity.interval^2*vcov(news.trust.fam.int)[8,8] + 2*familiarity.interval*vcov(news.trust.fam.int)[2,8])
news.trust.fam.int.marginal.point.upper <- news.trust.fam.int.marginal.point+2*news.trust.fam.int.marginal.se
news.trust.fam.int.marginal.point.lower <- news.trust.fam.int.marginal.point-2*news.trust.fam.int.marginal.se

#Effect at lowest level of familiarity
news.trust.fam.int.marginal.point[1]; news.trust.fam.int.marginal.point.upper[1]; news.trust.fam.int.marginal.point.lower[1]

#Effect at highest level of familiarity
news.trust.fam.int.marginal.point[100]; news.trust.fam.int.marginal.point.upper[100]; news.trust.fam.int.marginal.point.lower[100]

#Figure 2 - Panel A
pdf(file='familiarity-trust.pdf',height=4,width=5)
#par(mfrow=c(1,2))
par(mar=c(4,4,.25,.25))
plot(x=familiarity.interval,y=news.trust.fam.int.marginal.point,type='l',ylim=c(-.75,.1),xlim=c(-1.5,2),las=1,lwd=3,ylab="Marginal Effect of Local News Attack",xlab="Familiarity with Local News Source",main="",cex.axis=1.2,cex.lab=1.2)
abline(h=0,lty=2)
lines(x=familiarity.interval,y=news.trust.fam.int.marginal.point.upper)
lines(x=familiarity.interval,y=news.trust.fam.int.marginal.point.lower)

oo <- par()
# find out bounding for the plot
u <- par("usr")
# convert coordinates
v <- c(
  grconvertX(u[1:2], "user", "ndc"),
  grconvertY(u[3:4], "user", "ndc")
)
# bounding box for the histogram
v <- c(v[1], v[2], v[3], v[3] + (v[4]+v[3])*0.1)
# set plot area, no margins
oo <- par(fig=v, new=TRUE, mar=c(0,0,0,0))
# add histogram
hist(localtrust.combined$newspaper.familiarity, axes=FALSE, xlab="", ylab="", main="", breaks=15)
dev.off()

news.use.fam.int.marginal.point <- news.use.fam.int$coefficients[2] + news.use.fam.int$coefficients[8]*familiarity.interval
news.use.fam.int.marginal.se <- sqrt(vcov(news.use.fam.int)[2,2] + familiarity.interval^2*vcov(news.use.fam.int)[8,8] + 2*familiarity.interval*vcov(news.use.fam.int)[2,8])
news.use.fam.int.marginal.point.upper <- news.use.fam.int.marginal.point+2*news.use.fam.int.marginal.se
news.use.fam.int.marginal.point.lower <- news.use.fam.int.marginal.point-2*news.use.fam.int.marginal.se

#Point in Distribution where treatment effect becomes detectable
change.point <- familiarity.interval[min(which(news.use.fam.int.marginal.point.upper < 0))]
ecdf_fun <- function(x, perc) ecdf(x)(perc)
ecdf_fun(localtrust.combined$newspaper.familiarity,change.point)


tiff(height=4,width=5,file='familiarity-trust.tiff',res=300,units="in")
#par(mfrow=c(1,2))
par(mar=c(4,4,.25,.25))
plot(x=familiarity.interval,y=news.trust.fam.int.marginal.point,type='l',ylim=c(-.75,.1),xlim=c(-1.5,2),las=1,lwd=3,ylab="Marginal Effect of Local News Attack",xlab="Familiarity with Local News Source",main="",cex.axis=1.2,cex.lab=1.2)
abline(h=0,lty=2)
lines(x=familiarity.interval,y=news.trust.fam.int.marginal.point.upper)
lines(x=familiarity.interval,y=news.trust.fam.int.marginal.point.lower)

oo <- par()
# find out bounding for the plot
u <- par("usr")
# convert coordinates
v <- c(
  grconvertX(u[1:2], "user", "ndc"),
  grconvertY(u[3:4], "user", "ndc")
)
# bounding box for the histogram
v <- c(v[1], v[2], v[3], v[3] + (v[4]+v[3])*0.1)
# set plot area, no margins
oo <- par(fig=v, new=TRUE, mar=c(0,0,0,0))
# add histogram
hist(localtrust.combined$newspaper.familiarity, axes=FALSE, xlab="", ylab="", main="", breaks=15)
dev.off()


#Figure 2 - Panel B
pdf(file='familiarity-use.pdf',height=4,width=5)
#par(mfrow=c(1,2))
par(mar=c(4,4,.25,.25))
plot(x=familiarity.interval,y=news.use.fam.int.marginal.point,type='l',ylim=c(-.4,.1),xlim=c(-1.5,2),las=1,lwd=3,ylab="Marginal Effect of Local News Attack",xlab="Familiarity with Local News Source",main="", cex.axis=1.2,cex.lab=1.2)
abline(h=0,lty=2)
lines(x=familiarity.interval,y=news.use.fam.int.marginal.point.upper)
lines(x=familiarity.interval,y=news.use.fam.int.marginal.point.lower)

oo <- par()
# find out bounding for the plot
u <- par("usr")
# convert coordinates
v <- c(
  grconvertX(u[1:2], "user", "ndc"),
  grconvertY(u[3:4], "user", "ndc")
)
# bounding box for the histogram
v <- c(v[1], v[2], v[3], v[3] + (v[4]+v[3])*0.1)
# set plot area, no margins
oo <- par(fig=v, new=TRUE, mar=c(0,0,0,0))
# add histogram
hist(localtrust.combined$newspaper.familiarity, axes=FALSE, xlab="", ylab="", main="", breaks=15)
dev.off()

tiff(height=4,width=5,file='familiarity-use.tiff',res=300,units="in")
#par(mfrow=c(1,2))
par(mar=c(4,4,.25,.25))
plot(x=familiarity.interval,y=news.use.fam.int.marginal.point,type='l',ylim=c(-.4,.1),xlim=c(-1.5,2),las=1,lwd=3,ylab="Marginal Effect of Local News Attack",xlab="Familiarity with Local News Source",main="", cex.axis=1.2,cex.lab=1.2)
abline(h=0,lty=2)
lines(x=familiarity.interval,y=news.use.fam.int.marginal.point.upper)
lines(x=familiarity.interval,y=news.use.fam.int.marginal.point.lower)

oo <- par()
# find out bounding for the plot
u <- par("usr")
# convert coordinates
v <- c(
  grconvertX(u[1:2], "user", "ndc"),
  grconvertY(u[3:4], "user", "ndc")
)
# bounding box for the histogram
v <- c(v[1], v[2], v[3], v[3] + (v[4]+v[3])*0.1)
# set plot area, no margins
oo <- par(fig=v, new=TRUE, mar=c(0,0,0,0))
# add histogram
hist(localtrust.combined$newspaper.familiarity, axes=FALSE, xlab="", ylab="", main="", breaks=15)
dev.off()


#Populism as Moderator
news.trust.pop.int <- lm(local.newspaper.trust ~ treatment*populism.scale + factor(newspaper.reputation.pre), data=localtrust.combined)
news.use.pop.int <- lm(local.newspaper.use ~ treatment*populism.scale + factor(newspaper.use.pre), data=localtrust.combined)
politician.approval.populism.interaction <- lm(politician.approval.scale ~ treatment*populism.scale + factor(politician.reputation.pre), data=localtrust.combined)

#Table C1
stargazer(news.trust.pop.int,news.use.pop.int,keep=c('treatment','populism.scale'),covariate.labels=c('Local News Attack','Populism','Local News Attack X Populism'),dep.var.labels=c("Newspaper Reputation","Newspaper Use"),digits=1)

###
#Attack Spillover
###

cnn.trust <- lm(cnn.therm ~ treatment, data=localtrust.combined)
newspaper.trust <- lm(newspaper.therm ~ treatment, data=localtrust.combined)

#Table G1
stargazer(newspaper.trust,cnn.trust,covariate.labels=c("Attack Treatment"),digits=1,dep.var.labels=c("Local Newspaper Therm","CNN Therm"))

###
##In Market Analysis
###

trust.market <- lm(local.newspaper.trust ~ treatment + factor(newspaper.reputation.pre), data=localtrust.combined,subset=which(in.market==1))
use.market <- lm(local.newspaper.use ~ treatment + factor(newspaper.use.pre), data=localtrust.combined,subset=which(in.market==1))
trust.p.market <- lm(local.newspaper.trust ~ treatment*I(pid3pt_scale=="Republican") + factor(newspaper.reputation.pre), data=localtrust.combined,subset=which(in.market==1))
use.p.market <- lm(local.newspaper.use ~ treatment*I(pid3pt_scale=="Republican") + factor(newspaper.use.pre), data=localtrust.combined,subset=which(in.market==1))

#Appendix Table D1
stargazer(trust.market,use.market,trust.p.market,use.p.market,keep=c('treatment','Republican'),covariate.labels="Local News Attack",dep.var.labels=c("Media Reputation","Media Use"),digits=1)

approval.market <- lm(politician.approval.scale ~ treatment + factor(politician.reputation.pre), data=localtrust.combined,subset=which(in.market==1))
approval.p.market <- lm(politician.approval.scale ~ treatment*I(pid3pt_scale=="Republican") + factor(politician.reputation.pre), data=localtrust.combined,subset=which(in.market==1))

#Appendix Table D2
stargazer(approval.market,approval.p.market,keep=c('treatment','Republican'),covariate.labels=c('Local News Attack','Republican','Local News Attack X Republican'),column.labels=c("Main","PID as Moderator"),digits=1)

###
#Comparing Size of Partisan Divide to National News
###

newspaper.therm.control <- lm(newspaper.therm ~ I(pid3pt_scale=="Republican"), data=localtrust.combined,subset=which(pid3pt_scale!="Independent" & treatment==0))
newspaper.therm.treat <- lm(newspaper.therm ~ I(pid3pt_scale=="Republican"), data=localtrust.combined,subset=which(pid3pt_scale!="Independent" & treatment==1))
cnn.therm.regression <- lm(cnn.therm ~ I(pid3pt_scale=="Republican"), data=localtrust.combined,subset=which(pid3pt_scale!="Independent"))

local.party.divide.control <- newspaper.therm.control$coefficients[2]
local.party.divide.treatment <- newspaper.therm.treat$coefficients[2]
cnn.party.divide <- cnn.therm.regression$coefficients[2]

local.party.divide.control.se <- summary(newspaper.therm.control)$coefficients[2,2]
local.party.divide.treatment.se <- summary(newspaper.therm.treat)$coefficients[2,2]
cnn.party.divide.se <- summary(cnn.therm.regression)$coefficients[2,2]

party.divides.point <- c(cnn.party.divide,local.party.divide.control,local.party.divide.treatment)
party.divides.se <- c(cnn.party.divide.se,local.party.divide.control.se,local.party.divide.treatment.se)

party.divide.frame <- cbind.data.frame(party.divides.point,party.divides.se)
party.divide.frame$upper <- party.divide.frame[,1]+2*party.divide.frame[,2]
party.divide.frame$lower <- party.divide.frame[,1]-2*party.divide.frame[,2]

#Figure 1
pdf(height=4,width=6,file='partisan-trust-gaps.pdf')
par(mar=c(2.5,4,0,0))
barplot(party.divide.frame$party.divides.point,las=1,ylim=c(-36,2),names.arg=c("CNN\n","\nLocal Newspaper\n(Control)","\nLocal Newspaper\n(Treatment)"),main="",ylab="Rep-Dem Therm")
arrows(x0=c(.7,1.9,3.1),y0=party.divide.frame$upper,y1=party.divide.frame$lower,lwd=2,angle=90,code=3,length=.1)
dev.off()

tiff(height=4,width=6,file='partisan-trust-gaps.tiff',res=300,units="in")
par(mar=c(2.5,4,0,0))
barplot(party.divide.frame$party.divides.point,las=1,ylim=c(-36,2),names.arg=c("CNN\n","\nLocal Newspaper\n(Control)","\nLocal Newspaper\n(Treatment)"),main="",ylab="Rep-Dem Therm")
arrows(x0=c(.7,1.9,3.1),y0=party.divide.frame$upper,y1=party.divide.frame$lower,lwd=2,angle=90,code=3,length=.1)
dev.off()

###
#Appendix Figures of State-by-State Results
###

###
#Descriptives on Sample
###

localtrust.combined$republican <- ifelse(localtrust.combined$pid3pt_scale=="Republican",1,0)
localtrust.combined$democrat <- ifelse(localtrust.combined$pid3pt_scale=="Democrat",1,0)
localtrust.combined$independent <- ifelse(localtrust.combined$pid3pt_scale=="Independent",1,0)
localtrust.combined$female <- ifelse(localtrust.combined$sex=="Female",1,0)
localtrust.combined$hispanic <- ifelse(localtrust.combined$race=="Hispanic/Latino",1,0)
localtrust.combined$white <- ifelse(localtrust.combined$race=="White",1,0)
localtrust.combined$black <- ifelse(localtrust.combined$race=="African-American/Black",1,0)
localtrust.combined$other <- ifelse(localtrust.combined$race%in%c("Other","Asian or Asian-American","Native American",""),1,0)
localtrust.combined$college <- ifelse(localtrust.combined$edu %in% c("Advanced degree","Bachelor's degree"),1,0)
localtrust.combined$age <- as.numeric(as.character(localtrust.combined$age))
localtrust.combined$age[which(nchar(localtrust.combined$age) > 2)] <- NA

demo.frame <- localtrust.combined[,c('black','hispanic','white','other','college','female','age','democrat','republican','independent','survey.type')]
prolific.demo <- subset(demo.frame,demo.frame$survey.type=="prolific")
cloud.demo <- subset(demo.frame,demo.frame$survey.type=="cloud")

demo.frame$survey.type <- NULL
prolific.demo$survey.type <- NULL
cloud.demo$survey.type <- NULL

demo.frame <- apply(X=demo.frame,MARGIN=2,FUN=mean,na.rm=TRUE)
demo.frame <- as.data.frame(t(t(demo.frame)))
demo.frame <- round(demo.frame,digits=2)

prolific.frame <- apply(X=prolific.demo,MARGIN=2,FUN=mean,na.rm=TRUE)
prolific.frame <- as.data.frame(t(t(prolific.frame)))
prolific.frame <- round(prolific.frame,digits=2)

cloud.frame <- apply(X=cloud.demo,MARGIN=2,FUN=mean,na.rm=TRUE)
cloud.frame <- as.data.frame(t(t(cloud.frame)))
cloud.frame <- round(cloud.frame,digits=2)

sample.size <- c(dim(localtrust.combined)[1],dim(prolific.demo)[1],dim(cloud.demo)[1])

demo.frame <- cbind.data.frame(demo.frame,prolific.frame,cloud.frame)
demo.frame <- rbind.data.frame(demo.frame,sample.size)
names(demo.frame) <- c("Pooled","Prolific","PrimePanels")
rownames(demo.frame) <- c("Black","Hispanic","Non-Hispanic White","Other","College","Female","Age","Democrat","Republican","Independent","Sample Size")

#Appendix Table A1
xtable(demo.frame)

###
#Examining Effect Variation by State (Figures F1 and F2)
###

state.vector <- unique(localtrust.combined$statetext)
state.count <- ddply(localtrust.combined,.(statetext),summarise,obs.count=length(newspaper.consume))
state.count$large <- ifelse(state.count$obs.count >=50, 1, 0)
state.vector <- state.count$statetext[which(state.count$large==1)]

trust.loop.point <- NA
trust.loop.se <- NA

approval.loop.point <- NA
approval.loop.se <- NA

for(k in 1:length(state.vector)){
	
	newspaper.trust.loop <- lm(local.newspaper.trust ~ treatment + factor(newspaper.reputation.pre), data=localtrust.combined,subset=which(statetext==state.vector[k]))
	trust.loop.point[k] <- newspaper.trust.loop$coefficients[2]
	trust.loop.se[k] <- summary(newspaper.trust.loop)$coefficients[2,2]
	
	politician.approval.loop <- lm(politician.approval.scale ~ treatment + factor(politician.reputation.pre), data=localtrust.combined, subset=which(statetext==state.vector[k]))
	approval.loop.point[k] <- politician.approval.loop$coefficients[2]
	approval.loop.se[k] <-  summary(politician.approval.loop)$coefficients[2,2]
	
}

trust.frame <- cbind.data.frame(state.vector,trust.loop.point,trust.loop.se)
trust.frame$upper <- trust.frame[,2] + 1.96*trust.frame[,3]
trust.frame$lower <- trust.frame[,2] - 1.96*trust.frame[,3]

pdf(height=6,width=4,file='attack-trust-state.pdf')
par(mar=c(4,6.75,.5,.5))
plot(y=length(trust.loop.point):1,x=trust.loop.point,pch=16,yaxt='n',ylab='',xlim=c(-1,.2),xlab="Effect of Attack on Trust",cex=1.5)
axis(side=2,at=length(trust.loop.point):1,labels=state.vector,las=1)
segments(x0=trust.frame$lower,x1=trust.frame$upper,y0=length(trust.loop.point):1,lwd=3)
abline(v=0,lty=2)
abline(v=news.trust.main$coefficients[2],lty=1,lwd=3)
dev.off()

approval.frame <- cbind.data.frame(state.vector,approval.loop.point,approval.loop.se)
approval.frame$upper <- approval.frame[,2] + 1.96*approval.frame[,3]
approval.frame$lower <- approval.frame[,2] - 1.96*approval.frame[,3]

pdf(height=6,width=4,file='politician-reputation-state.pdf')
par(mar=c(4,6.75,.5,.5))
plot(y=length(approval.loop.point):1,x=approval.loop.point,pch=16,yaxt='n',ylab='',xlim=c(-.6,.4),xlab="Effect of Attack on Politician Approval",cex=1.5)
axis(side=2,at=length(approval.loop.point):1,labels=state.vector,las=1)
segments(x0=approval.frame$lower,x1=approval.frame$upper,y0=length(approval.loop.point):1,lwd=3)
abline(v=0,lty=2)
abline(v=pol.approval.main$coefficients[2],lty=1,lwd=3)
dev.off()


###
#Code used to create Index Variables is shown below 
###

#localtrust.combined$newspaper.familiarity <- prcomp(localtrust.combined[,c('newspaper.reputation.strength','newspaper.use.pre')],center=TRUE,scale=TRUE)$x[,1]
#localtrust.combined$newspaper.familiarity <- localtrust.combined$newspaper.familiarity/sd(localtrust.combined$newspaper.familiarity,na.rm=TRUE)
#localtrust.combined$newspaper.familiarity <- -localtrust.combined$newspaper.familiarity

#localtrust.combined$populism.scale <- prcomp(localtrust.combined[,c('populism1','populism2','populism3','populism4','populism5','populism6')],center=TRUE,scale=TRUE)$x[,1]
#localtrust.combined$populism.scale <- localtrust.combined$populism.scale/sd(localtrust.combined$populism.scale,na.rm=TRUE)

summary(prcomp(localtrust.combined[,c('trust1','trust2','trust3','trust4','trust5','bias','newspaper.therm')],center=TRUE,scale=TRUE))
prcomp(localtrust.combined[,c('trust1','trust2','trust3','trust4','trust5','bias','newspaper.therm')],center=TRUE,scale=TRUE)$rotation #Appendix Table A4
#localtrust.combined$local.newspaper.trust <- prcomp(localtrust.combined[,c('trust1','trust2','trust3','trust4','trust5','bias','newspaper.therm')],center=TRUE,scale=TRUE)$x[,1]
#localtrust.combined$local.newspaper.trust <- localtrust.combined$local.newspaper.trust/sd(localtrust.combined$local.newspaper.trust,na.rm=TRUE)

summary(prcomp(localtrust.combined[,c('newspaper.choice','newspaper.consume','newspaper.subscribe')],center=TRUE,scale=TRUE))
prcomp(localtrust.combined[,c('newspaper.choice','newspaper.consume','newspaper.subscribe')],center=TRUE,scale=TRUE)$rotation #Appendix Table A5
#combined$local.newspaper.use <- prcomp(combined[,c('newspaper.choice','newspaper.consume','newspaper.subscribe')],center=TRUE,scale=TRUE)$x[,1]
#combined$local.newspaper.use <- combined$local.newspaper.use/sd(combined$local.newspaper.use,na.rm=TRUE)
#combined$local.newspaper.use <- -combined$local.newspaper.use

summary(prcomp(localtrust.combined[,c('politician.favorability','politician.approval.post','politician.therm')],center=TRUE,scale=TRUE))
prcomp(localtrust.combined[,c('politician.favorability','politician.approval.post','politician.therm')],center=TRUE,scale=TRUE)$rotation #Appendix Table A6
#localtrust.combined$politician.approval.scale <- prcomp(localtrust.combined[,c('politician.favorability','politician.approval.post','politician.therm')],center=TRUE,scale=TRUE)$x[,1]
#localtrust.combined$politician.approval.scale <- localtrust.combined$politician.approval.scale/sd(localtrust.combined$politician.approval.scale,na.rm=TRUE)
#localtrust.combined$politician.approval.scale <- -localtrust.combined$politician.approval.scale

